import md5 from 'md5'

const FALSE_KEY = ['file[]', 'files[]']
const FALSE_VALUE = [undefined, null, NaN]
/**
 * @description: 接口签名秘钥
 * @param {Record<string, any> | null} params 接口请求参数
 * @return {string} md5加密字符串
 */
export default function toEncryption(params: Record<string, any> | null): string {
  let result = ''
  const SECRET: string = import.meta.env.VITE_APP_SECRET || ''
  if (params) {
    // 过滤假值及file类型
    const objFilter = {}
    Object.keys(params)
      .filter((key) => !FALSE_KEY.includes(key))
      .forEach((key) => {
        if (!FALSE_VALUE.includes(params[key])) objFilter[key] = params[key]
      })
    // 倒序排列
    const keys = Object.keys(objFilter).sort().reverse()
    for (const key of keys) result += `${key}${params[key] as string}`
  }
  if (SECRET) {
    result += SECRET
  }
  return md5(result)
}
